/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package TCPprotocolo;

import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;

/**
 *
 * @author emanuellima
 */
public class TextMessage extends Message {
    
    public TextMessage (String text){
        this.type = 1;
        this.text = text;
    }
    private String text;

    /**
     * @return the text
     */
    public String getText() {
        return text;
    }

    /**
     * @param text the text to set
     */
    public void setText(String text) {
        this.text = text;
    }
    
     public ByteArrayOutputStream encoderTextMessage(String message) throws IOException{
        
        ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
        DataOutputStream dataOut = new DataOutputStream(byteOut);
        dataOut.writeByte(type);
        dataOut.writeChars(message);
        
        
        return byteOut;
    }
    
    public void sendMessage(ByteArrayOutputStream message, Socket socket) throws IOException{
        
        byte msg [] = message.toByteArray();
        DataOutputStream out = new DataOutputStream(socket.getOutputStream());  
        out.writeInt(msg.length); //Agora sim, o tamanho da mensagem  
        out.write(msg); //Os dados  
        out.flush();
    }
    
}
